package com.sbl.bajw.pc.action;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sbl.bajw.common.ExcelUtil;
import com.sbl.bajw.common.web.JsonResultUtil;
import com.sbl.bajw.common.web.MessageException;
import com.sbl.bajw.common.web.RequestUnit;
import com.sbl.bajw.pc.model.LogTModel;
import com.sbl.bajw.pc.model.PrizeAlarmModel;
import com.sbl.bajw.pc.service.LogTModelService;
import com.sbl.bajw.pc.service.PrizeAlarmModelService;
import com.sbl.bajw.util.DateUtil;
import com.sbl.bajw.util.JsonUtil;
import com.sbl.bajw.util.LogUtil;

@Controller
@RequestMapping("/PrizeAlarm")
public class PrizeAlarmModelAction {

	@Resource
	private PrizeAlarmModelService service;

	@Resource
	private LogTModelService logService;
	
	static Logger log;
	
	static{
		log = LogUtil.getLogger("PrizeAlarm");
	}
	
	@RequestMapping(value = "/insert")
	public @ResponseBody String insert(HttpServletResponse response, HttpServletRequest request) throws MessageException{
		PrizeAlarmModel model = RequestUnit.getModel(PrizeAlarmModel.class);
		String json = "";
		model.setIsQuelity("4");
		try {
			log.info(model.getCreateBy() + "上传文件");
			
			//积分入库
			LogTModel logT = new LogTModel();
			logT.setUserid(model.getCreateBy());
			logT.setCode("12");
			
			service.insert(model);
			logService.insert(logT);
			
			json = JsonResultUtil.Success();
		} catch (Exception e) {
			log.info(e.getMessage());
			e.printStackTrace();
			throw new MessageException("上传失败", "3");
		}
		return json;
	}
	
	/**
	 * @标题：createApp
	 * @作者：罗琦
	 * @时间：2015年5月25日
	 * @作用：随手拍数据入库
	 * @param response
	 * @param request
	 * @return
	 * @throws MessageException
	 */
	@RequestMapping(value = "/createApp")
	public @ResponseBody String createApp(HttpServletResponse response, HttpServletRequest request) throws MessageException{
		PrizeAlarmModel model = RequestUnit.getModel(PrizeAlarmModel.class);
		String json = "";
		model.setIsQuelity("4");
		try {
			log.info(model.getCreateBy() + "上传文件");
			service.insert(model);
			
			//积分入库
			LogTModel logT = new LogTModel();
			logT.setUserid(model.getCreateBy());
			logT.setCode("12");
			logService.insert(logT);
			
			json = JsonResultUtil.Success();
		} catch (Exception e) {
			log.info(e.getMessage());
			e.printStackTrace();
			throw new MessageException("上传失败", "3");
		}
		return json;
	}
	
	/**
	 * @标题：List
	 * @作者：罗琦
	 * @时间：2015年5月12日
	 * @作用：随手拍列表页面跳转
	 * @return
	 */
	@RequestMapping(value = "/List")
	public ModelAndView List(){
		ModelAndView mav = new ModelAndView("view/manager/prizeAlarm/prizeAlarmList");
		return mav;
	}
	
	/**
	 * @标题：detail
	 * @作者：罗琦
	 * @时间：2015年5月12日
	 * @作用：随手拍详情页面跳转
	 * @return
	 */
	@RequestMapping(value = "/detail")
	public ModelAndView detail(){
		String id = RequestUnit.getString("id");
		HashMap<String, Object> model = service.getModel(id);
		List<HashMap<String, Object>> list = service.getAttfileByPrizeAlarmId(id);
		HashMap<String, Object> voice = service.getAttfileMp3(list);
		RequestUnit.setAttr("model", model);
		RequestUnit.setAttr("pics", list);
		RequestUnit.setAttr("voice", voice);
		ModelAndView mav = new ModelAndView("view/manager/prizeAlarm/detail");
		return mav;
	}
	
	/**
	 * @标题：getPrizeAlarmList
	 * @作者：罗琦
	 * @时间：2015年5月12日
	 * @作用：随手拍裂变展示
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getPrizeAlarmList")
	public @ResponseBody String getPrizeAlarmList(HttpServletResponse response, HttpServletRequest request){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String json = service.getPrizeAlarmList(parm);
		return json;
	}
	
	/**
	 * @标题：downloadPrizeAlarmList
	 * @作者：罗琦
	 * @时间：2015年5月12日
	 * @作用：随手拍数据导出
	 * @param response
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping(value = "/downloadPrizeAlarmList")
	public void downloadPrizeAlarmList(HttpServletResponse response, HttpServletRequest request) throws Exception{
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		List<HashMap<String, Object>> list = service.downloadPrizeAlarmList(parm);
		LinkedHashMap<String, String> column = new LinkedHashMap<String, String>();
		column.put("ID", "编号");
		column.put("ADDR", "报警地址");
		column.put("CONTENT", "报警内容");
		column.put("MAPX", "报警坐标经度");
		column.put("MAPY", "报警坐标纬度");
		column.put("SHOWNAME", "报警人");
		column.put("CREATE_TIME", "报警时间");
		
		HSSFWorkbook wb = ExcelUtil.createExcel(list, column);
		OutputStream outStream = response.getOutputStream();
		response.setContentType("application/x-msdownload");  
		response.setHeader("Content-Disposition","attachment;filename=prize_alarm.xls");  
		wb.write(outStream);
	}
	
	/**
	 * @标题：getListApp
	 * @作者：罗琦
	 * @时间：2015年6月10日
	 * @作用：APP随手拍列表
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getListApp")
	public @ResponseBody String getListApp(HttpServletResponse response, HttpServletRequest request){
		HashMap<String, Object> parm = RequestUnit.getHashMapParmO();
		String start = DateUtil.getMondayDate();
		String stop = DateUtil.getSundayDate();
		parm.put("startdate", start + " 00:00:00");
		parm.put("stopdate", stop + "23:59:59");
		
		List<HashMap<String, Object>> list = service.getListApp(parm);
		int num = service.getCountApp(parm);
		String result = "{\"num\":" + num + ",\"list\":" + JsonUtil.ConvertArrayToJson(list) + "}";
		String json = JsonResultUtil.Success("", JsonUtil.ConvertObjectToJson(result));
		return json;
	}
	
	/**
	 * @标题：detailApp
	 * @作者：罗琦
	 * @时间：2015年6月10日
	 * @作用：APP随手拍详情
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/detailApp")
	public @ResponseBody String detailApp(HttpServletResponse response, HttpServletRequest request){
		HashMap<String, String> parm = RequestUnit.getHashMapParmS();
		PrizeAlarmModel model = service.detailApp(parm);
		String json = JsonResultUtil.Success(JsonUtil.ConvertObjectToJson(model));
		return json;
	}
}
